<template>
  <div class="car-container">
    <div class="list">
      <div class="mui-card" v-for="(item, i) in goodsList" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <!-- 开关 -->
            <el-switch
              v-model="$store.getters.getSelected[item.id]"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="getSelected($store.getters.getSelected[item.id],item.id)"
            >
            </el-switch>
            <img :src="item.thumb_path" alt="" />
            <div class="info">
              <h3>{{ item.title }}</h3>
              <p>
                <span class="price">￥{{ item.sell_price }}</span>
                <numbox
                  :initcount="$store.getters.getGoodsCount[item.id]"
                  :goodsId="item.id"
                ></numbox>
                <a href="#" @click.prevent="remove(item.id, i)">删除 </a>
              </p>
            </div>
          </div>
        </div>
      </div>

      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner jiesuan">
            <div class="messinfo">
              <p>总计（不含运费）</p>
              <p>已勾选商品 <span class="price">{{ $store.getters.getGoodsCountAndAmount.count }}</span> 件，总价￥<span class="price">{{ $store.getters.getGoodsCountAndAmount.amount }}</span></p>
            </div>
            <el-button type="danger" size="small">去结算</el-button>
          </div>
        </div>
      </div>
    </div>
    <p>{{ $store.getters.getSelected }}</p>
  </div>
</template>

<script>
import numbox from "../subcomponents/carNumBox.vue";

export default {
  data() {
    return {
      goodsList: [],
    };
  },

  created() {
    this.getCarGoods();
  },

  methods: {
    getCarGoods() {
      var idArr = [];

      //得到car中的id
      this.$store.state.car.forEach((item) => idArr.push(item.id));

      if (idArr.length <= 0) {
        return;
      }

      //以逗号拼接在接口地址后面
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then((result) => {
          if (result.body.status === 0) {
            this.goodsList = result.body.message;
          }
        });
    },

    remove(id, index) {
      this.goodsList.splice(index, 1);

      this.$store.commit("removeCar", id);
    },

    getSelected(val,id){

      this.$store.commit("updateSelected",{

          id : id , selectStatus: val

      })
      console.log(val+"--------"+id )

    }



  },

  components: {
    numbox,
  },
};
</script>

<style lang="scss" scoped>
.car-container {
  background-color: lightgrey;

  overflow: hidden;

  .list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }

    img {
      width: 80px;
      height: 80px;
      margin: 0 5px;
    }

    .info {
      h3 {
        font-size: 14px;
        font-weight: bold;
        margin: 10px 0;
      }

      .price {
        color: lightcoral;
        font-weight: bold;
      }
    }

    .jiesuan{
      display: flex;
      justify-content: space-between;
      align-items: center;

      .price{
        color: lightcoral
      }
    }
  }
}
</style>